<?php


namespace App\Commons\Utils\ModelTools;


use Illuminate\Database\Eloquent\Builder;

class BaseEloquentBuilder extends Builder
{
    public function alias(string $alias)
    {
        return ($this->from($this->getModel()->getTable() . " as $alias"));
    }

    /**
     * @param PaginateQueryModel $paginateQueryModel
     *
     * @return array
     */
    public function jsonPaginate(PaginateQueryModel $paginateQueryModel)
    {
        $count    = $this->count();
        $pageList = [];
        if ($count > 0) {
            $pageList = $this->forPage($paginateQueryModel->getPage(), $paginateQueryModel->getPerPage())->get();
        }

        return [
            'total_count' => $count,
            'list'        => $pageList,
        ];
    }
}